<template>
    <div class="${className?uncap_first}">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
            <el-form-item>
                <el-input v-model="dataForm.key" clearable placeholder="参数名"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="getDataList()">查询</el-button>
                <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
                <el-button :disabled="dataListSelections.length <= 0" type="danger" @click="deleteHandle()">批量删除</el-button>
            </el-form-item>
        </el-form>
        <el-table
                v-loading="dataListLoading"
                :data="dataList"
                border
                style="width: 100%;"
                @selection-change="selectionChangeHandle">
            <el-table-column
                    align="center"
                    header-align="center"
                    type="selection"
                    width="50">
            </el-table-column>
            <#list columns as column>
                <el-table-column
                        align="center"
                        header-align="center"
                        label="${column.comment}"
                        prop="${column.attrName}">
                </el-table-column>
            </#list>
            <el-table-column
                    align="center"
                    fixed="right"
                    header-align="center"
                    label="操作"
                    width="150">
                <template slot-scope="scope">
                    <el-button size="small" type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                    <el-button size="small" type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                :current-page="pageIndex"
                :page-size="pageSize"
                :page-sizes="[10, 20, 50, 100]"
                :total="totalCount"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
</template>

<script>
    import AddOrUpdate from './${className}AddOrUpdate'
    import request from "@/utils/request";

    export default {
        name: "${className}",
        created() {
            // 初始化数据
            this.getDataList();
        },
        data() {
            return {
                dataForm: {
                    // 主键
                    key: ''
                },
                dataList: [],
                pageIndex: 1,
                pageSize: 10,
                totalCount: 0,
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false
            }
        },
        components: {
            AddOrUpdate
        },
        activated() {
            this.getDataList()
        },
        methods: {
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true
                request({
                    url: '<#if moduleName != "">/${moduleName}</#if>/${className?uncap_first}/list',
                    method: 'post',
                    data: {
                        'currentPage': this.pageIndex,
                        'pageSize': this.pageSize,
                        'key': this.dataForm.key
                    }
                }).then(({data}) => {
                    if (data && data.code === 200) {
                        this.dataList = data.data.records
                        this.totalCount = Number(data.data.total)
                    } else {
                        this.dataList = []
                        this.totalCount = 0
                    }
                    this.dataListLoading = false
                })
            },
            // 每页数
            sizeChangeHandle(val) {
                this.pageSize = val
                this.pageIndex = 1
                this.getDataList()
            },
            // 当前页
            currentChangeHandle(val) {
                this.pageIndex = val
                this.getDataList()
            },
            // 多选
            selectionChangeHandle(val) {
                this.dataListSelections = val
            },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                let ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.id
                })
                this.$confirm('确定对[${pk.attrName}=' + ids.join(',') + ']进行[' + id ? '删除' : '批量删除]操作?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    request({
                        url: '<#if moduleName != "">/${moduleName}</#if>/${className?uncap_first}/delete',
                        method: 'post',
                        data: ids
                    }).then(({data}) => {
                        if (data && data.code === 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getDataList()
                                }
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
                })
            }
        }
    }
</script>

<style scoped>

</style>
